package com.ruoyi.file.utils;

import java.io.*;
import java.util.Arrays;

/**
 * @author Ww
 * @date 2023/3/13
 */
public class FileEn1 {
    public static File EnFile(InputStream srcInput, File tarFile) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        //加密文件
        File file2 = tarFile;
        try {
            InputStream is = srcInput;
            OutputStream os = new FileOutputStream(file2);
            bis = new BufferedInputStream(is);
            bos = new BufferedOutputStream(os);
            byte[] data = new byte[100];
            int len = 0;
            while ((len = bis.read(data)) != -1) {
                byte[] temp = Arrays.copyOfRange(data, 0, len);
                System.out.println("加密读取：" + Arrays.toString(temp));
                bos.write(reverseArray(temp));
                System.out.println("加密写入：" + Arrays.toString(temp));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bis != null) {
                    /* 关闭管子 */
                    bis.close();
                    bis = null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (bos != null) {
                    /* 关闭管子 */
                    bos.close();
                    bos = null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return file2;
    }

    //解密方法
    public static void DecFile(File srcFile, File tarFile) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        //源加密文件
        File file1 = srcFile;
        //解密文件
        File file2 = tarFile;
        try {
            InputStream is = new FileInputStream(file1);
            OutputStream os = new FileOutputStream(file2);
            bis = new BufferedInputStream(is);
            bos = new BufferedOutputStream(os);
            byte[] data = new byte[100];
            int len = 0;
            while ((len = bis.read(data)) != -1) {
                byte[] temp = Arrays.copyOfRange(data, 0, len);
                System.out.println("解密读取：" + Arrays.toString(temp));
                bos.write(reverseArray(temp));
                System.out.println("解密写入：" + Arrays.toString(temp));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bis != null) {
                    /* 关闭管子 */
                    bis.close();
                    bis = null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (bos != null) {
                    /* 关闭管子 */
                    bos.close();
                    bos = null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 字节数组的各位取反，并倒置
     *
     * @param bytes
     * @return
     */

    public static byte[] reverseArray(byte[] bytes) {
        for (int i = 0; i < bytes.length; i++) {
            byte b = (byte) ~bytes[i];
            bytes[i] = (byte) ~bytes[bytes.length - i - 1];
            bytes[bytes.length - i - 1] = b;
        }
        return bytes;

    }

}
